package com.hjm.project.agentInfo.agentInfo.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.constant.DictConstants;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.request.EssentialFactors;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.cache.RedisCache;
import com.hjm.project.ActActive.ActActive.domain.ActActive;
import com.hjm.project.ActActive.ActActive.service.IActActiveService;
import com.hjm.project.accounts.accounts.domain.Accounts;
import com.hjm.project.accounts.accounts.service.IAccountsService;
import com.hjm.project.agentInfo.agentActActive.domain.AgentActActive;
import com.hjm.project.agentInfo.agentActActive.service.IAgentActActiveService;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfoExport;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfoNextVo;
import com.hjm.project.agentInfo.agentInfo.utils.Utils;
import com.hjm.project.agentInfo.beautifulRecord.service.IBeautifulRecordService;
import com.hjm.project.agentInfo.point.domain.TaxPoint;
import com.hjm.project.agentInfo.point.service.ITaxPointService;
import com.hjm.project.area.domain.Area;
import com.hjm.project.area.service.IAreaService;
import com.hjm.project.bounty.bounty.domain.SpecialBounty;
import com.hjm.project.bounty.bounty.service.ISpecialBountyService;
import com.hjm.project.clientImg.clientImg.domain.ClientImg;
import com.hjm.project.salesman.salesman.service.ISalesmanService;
import com.hjm.project.system.user.domain.User;
import com.hjm.project.system.user.service.IUserService;
import com.yxhd.encrypt.EncryptUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.service.IAgentInfoService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 服务商申请Controller
 *
 * @author LOVE
 * @date 2020-11-18
 */
@Controller
@RequestMapping("/agentInfo/agentInfo")
public class AgentInfoController extends BaseController
{
    private String prefix = "agentInfo/agentInfo";

    @Resource
    private IAgentInfoService agentInfoService;             //服务商
    @Resource
    private IUserService userService;                       //用户
    @Resource
    private IAccountsService iAccountsService;              //账户
    @Resource
    private ISalesmanService salesmanService;               //业务员
    @Resource
    private IAreaService areaService;                       //区域
    @Resource
    private ITaxPointService taxPointService;               //税点
    @Resource
    private ISpecialBountyService iSpecialBountyService;    //引流奖励
    @Resource
    private IBeautifulRecordService beautifulRecordService; //靓号变更记录
    @Resource
    private IAgentActActiveService agentActActiveService;
    @Resource
    private IActActiveService actActiveService;
    @Autowired
    private RedisCache redisCache;

    @RequiresPermissions("agentInfo:agentInfo:view")
    @GetMapping()
    public String agentInfo()
    {
        return prefix + "/agentInfo";
    }

    /**
     * 导出正式服务商列表
     */
    @RequiresPermissions("agentInfo:agentInfo:exportExcel")
    @Log(title = "服务商导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportExcel")
    @ResponseBody
    public AjaxResult exportExcel(AgentInfo agentInfo)
    {
        List<AgentInfoExport> list = agentInfoService.selectAgentInfoExportList(agentInfo);
        ExcelUtil<AgentInfoExport> util = new ExcelUtil<AgentInfoExport>(AgentInfoExport.class);
        return util.exportExcel(list, "agentInfo");
    }

    /**
     * 导出正式服务商列表
     */
    @Log(title = "服务商导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAgent")
    @ResponseBody
    public AjaxResult exportAgent(@RequestBody String agentNum)
    {
        agentNum = agentNum.substring(0,agentNum.length()-1);
        List<AgentInfoNextVo> list = agentInfoService.selectAgentInfoNext(agentNum);
        ExcelUtil<AgentInfoNextVo> util = new ExcelUtil<AgentInfoNextVo>(AgentInfoNextVo.class);
        return util.exportExcel(list, "agentInfoNext");
    }

    /**
     * 查询服务商列表(申请)
     */
    @RequiresPermissions("agentInfo:agentInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgentInfo agentInfo)
    {
        startPage();
        if (StringUtils.isEmpty(agentInfo.getAgentStatus())) {
            agentInfo.setAgentStatus("1,3");
        } else {
            agentInfo.setAgentStatus(agentInfo.getAgentStatus());
        }
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }



    /**
     * 服务商上下级
     */
    @GetMapping("/supervisorAgentInfo")
    public String supervisorAgentInfo(ModelMap map,AgentInfo agentInfo) {
        map.put("agentNum",agentInfo.getAgentNum());
        map.put("belongAgent",agentInfo.getBelongAgent());
        return prefix + "/supervisorAgentInfo";
    }




    /**
     * 查询服务商申请列表(审核中)
     */
    @RequiresPermissions("agentInfo:agentInfo:list")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(AgentInfo agentInfo) {
        startPage();
        agentInfo.setAgentStatus("2");
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }


    /**
     * 查询服务商申请列表(正式)
     */
    @RequiresPermissions("agentInfo:agentInfo:list")
    @PostMapping("/list2")
    @ResponseBody
    public TableDataInfo list2(AgentInfo agentInfo) {
        startPage();
        agentInfo.setAgentStatus("0");
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }

    /**
     * 查询服务商申请列表(正式)
     */
    @RequiresPermissions("agentInfo:agentInfo:list")
    @PostMapping("/list3")
    @ResponseBody
    public TableDataInfo list3(AgentInfo agentInfo) throws Exception {
        startPage();
        if (agentInfo != null && StringUtils.isNotEmpty(agentInfo.getIdentityNum())) {
            agentInfo.setIdentityNum(EncryptUtils.getEncrypt(agentInfo.getIdentityNum()));
        }

        if (ShiroUtils.isAgent()) {
            agentInfo.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }

        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }


    /**
     * 服务商列表上下级
     */
    @RequiresPermissions("agentInfo:agentInfo:list")
    @PostMapping("/list4")
    @ResponseBody
    public TableDataInfo list4(AgentInfo agentInfo,@RequestParam Map<String,String> params) throws Exception {
        startPage();
        if (agentInfo != null && StringUtils.isNotEmpty(agentInfo.getIdentityNum())) {
            agentInfo.setIdentityNum(EncryptUtils.getEncrypt(agentInfo.getIdentityNum()));
        }

        //如果是从上下级页面直接搜索
        if (StringUtils.isNotEmpty(params.get("searchAgentNum")) && StringUtils.isNotEmpty(agentInfo.getAgentNum())) {
            agentInfo.setAgentNum(params.get("searchAgentNum"));
        }

        if (StringUtils.isNotEmpty(params.get("searchAgentNum")) && agentInfo.getBelongAgent() != null) {
            agentInfo.setBelongAgent(Long.parseLong(params.get("searchAgentNum")));
        }

        //如果是查看上级
        if (StringUtils.isNotEmpty(agentInfo.getAgentNum())) {
            //获取上级
            AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
            Long belongAgent = agentInfo1.getBelongAgent();
            agentInfo.setAgentNum(belongAgent.toString());
        }

        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }




    /**
     * 导出服务商申请列表
     */
    @RequiresPermissions("agentInfo:agentInfo:export")
    @Log(title = "服务商申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgentInfo agentInfo)
    {
        List<AgentInfo> list = agentInfoService.selectAgentInfoList(agentInfo);
        ExcelUtil<AgentInfo> util = new ExcelUtil<AgentInfo>(AgentInfo.class);
        return util.exportExcel(list, "agentInfo");
    }

    /**
     * 新增服务商申请
     */
    @GetMapping("/add")
    public String add(ModelMap map,AgentInfo agentInfo,@RequestParam(required = false) String type) throws Exception {
        if (agentInfo.getAgentNum() == null) {
            map.put("users",ShiroUtils.getSysUser());
            String startDate = DateUtils.getDate();
            String endDate = DateUtils.getYear(DateUtils.YYYY_MM_DD, 2);
            map.put("beginValid",startDate);
            map.put("endValid",endDate);
            return prefix + "/add";
        }

        //获取服务商信息
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
        //用户信息
        User user = new User();
        user.setObjNo(agentInfo1.getAgentNum());
        User user1 = userService.selectUser(user);
        Accounts acc = new Accounts();
        //解密法人身份证号
        if (agentInfo1.getIdentityNum() != null) {
            agentInfo1.setIdentityNum(EncryptUtils.getDecrypt(agentInfo1.getIdentityNum()));
        }

        //手机号加*
        agentInfo1.setLinkPhoneEncryption(agentInfo1.getLinkPhone().substring(0,3)+"****"+agentInfo1.getLinkPhone().substring(8,11));

        //app来源资料不全 只返回基本信息
        if ("0".equals(agentInfo1.getStatus()) && !"0".equals(agentInfo1.getAgentStatus())) {
            map.put("agentInfo",agentInfo1);
            //用户登录账号
            //map.put("adminName",user1.getLoginName());
            map.put("accounts",acc);
            return prefix + "/edit";
        }
        acc.setSeqId(agentInfo1.getAgentNum());
        Accounts accounts = iAccountsService.selectAccounts(acc);
        //解密开户银行账户screenNum
        accounts.setScreenNum(EncryptUtils.getDecrypt(accounts.getScreenNum()));
        accounts.setResphone(EncryptUtils.getDecrypt(accounts.getResphone()));
        if (accounts.getScreenIdnum() != null) {
            accounts.setScreenIdnum(EncryptUtils.getDecrypt(accounts.getScreenIdnum()));
        }
        //服务商信息
        map.put("agentInfo",agentInfo1);

        //服务商的注册区域
        String province = agentInfo1.getProvince();
        String city = agentInfo1.getCity();
        String a = agentInfo1.getArea();
        map.put("province",province);
        map.put("city",city);
        map.put("area",a);

        //账户的区域
        String bankAddress = accounts.getBankAddress();
        String province1 = "";
        String city1 = "";
        String a1 = "";
        if (StringUtils.isNotEmpty(bankAddress)) {
            String[] split1 = accounts.getBankAddress().split(",");
            province1 = split1[0];
            city1 = split1[1];
            a1 = split1.length == 3 ? split1[2] : "";
            map.put("province1",province1);
            map.put("city1",city1);
            map.put("area1",a1);
        }

        //区域信息
        List<Area> pList = new ArrayList<>();
        List<Area> cList = new ArrayList<>();
        List<Area> aList = new ArrayList<>();
        List<Area> pList1 = new ArrayList<>();
        List<Area> cList1 = new ArrayList<>();
        List<Area> aList1 = new ArrayList<>();
        List<Area> areas =redisCache.getCacheList(DictConstants.getAreaKey());
        if (areas==null){
            areas = areaService.selectAreaList(null);
            redisCache.setCacheList(DictConstants.getAreaKey(), areas);
        }
        for (Area area : areas) {
            if (area.getAreaType() == 0) {
                pList.add(area);
                pList1.add(area);
            } else if (area.getAreaType() == 1  && province.equals(area.getParentId())){
                cList.add(area);
            } else if (area.getAreaType() == 2 && city.equals(area.getParentId()) ){// && agentInfo1.getArea().equals(area.getParentId())
                aList.add(area);
            }

            if (area.getAreaType() == 1  && province1.equals(area.getParentId())){
                cList1.add(area);
            } else if (area.getAreaType() == 2 && city1.equals(area.getParentId()) ){// && agentInfo1.getArea().equals(area.getParentId())
                aList1.add(area);
            }

        }
        map.put("pList",pList);
        map.put("cList",cList);
        map.put("aList",aList);

        map.put("pList1",pList1);
        map.put("cList1",cList1);
        map.put("aList1",aList1);



        //账户信息
        map.put("accounts",accounts);
        return prefix + "/edit";
    }


    @PostMapping("/update")
    @RequiresPermissions("agentInfo:agentInfo:edit")
    @Log(title = "修改服务商", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult update(@RequestParam Map param, AgentInfo agent, Accounts accounts) throws Exception {
        accounts.setSeqId(agent.getAgentNum());
        //账户地址
        accounts.setAreaName(param.get("areaName1").toString());
        //地址代号
        accounts.setBankAddress(param.get("province1") + "," + param.get("city1") + "," + param.get("area1"));
        //手机号加*
        String phone = accounts.getShowResphone();
        accounts.setShowResphone(phone.substring(0,3)+"****"+phone.substring(8,11));
        //手机号加密
        accounts.setResphone(EncryptUtils.getEncrypt(phone));
        if ("2".equals(String.valueOf(accounts.getNature()))) {//对私
            if (!agent.getLinkMan().equals(accounts.getScreenName())) {
                throw new BusinessException("结算人和法人不一致");
            }
            String screenIdnum = accounts.getShowScreenidnum();
            String showScreennum = accounts.getShowScreennum();
            accounts.setShowScreennum(Utils.hiddenCard(showScreennum));
            accounts.setScreenNum(EncryptUtils.getEncrypt(showScreennum));
            accounts.setScreenName(accounts.getScreenName());
            accounts.setScreenIdnum(EncryptUtils.getEncrypt(screenIdnum));
            accounts.setShowScreenidnum(screenIdnum.substring(0, 10) + "****" + screenIdnum.substring(14));
        }
        //更新账户信息
        int i1 = iAccountsService.updateAccounts(accounts);
       /* agent.setShowIdentitynum(agent.getIdentityNum().substring(0, 10) + "****" + agent.getIdentityNum().substring(14));
        agent.setIdentityNum(EncryptUtils.getEncrypt(agent.getIdentityNum()));*/
        //更新服务商信息
        //销售经理
        agent.setMaintain(String.valueOf(param.get("sm_num1")));
        //运营经理
        agent.setSignSale(String.valueOf(param.get("sm_num2")));
        int i = agentInfoService.updateAgent(agent);
        if (i  > 0 && i1 > 0) {
            return success("操作成功");
        }
        return error("操作失败");
    }

    @PostMapping("/refresh")
    @RequiresPermissions("agentInfo:agentInfo:edit")
    @Log(title = "重新申请", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult refresh(AgentInfo agent){
        //修改被打回的服务商为重新申请
        agent.setAgentStatus("1");
        return toAjax(agentInfoService.updateAgent(agent));
    }

    /**
     * 新增保存服务商申请
     */
    @RequiresPermissions("agentInfo:agentInfo:add")
    @Log(title = "服务商申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam Map param, AgentInfo agentInfo, Accounts accounts) throws Exception {
        try {
            //账户类型(对公或对私 必选)
            //对公没有结算身份证号
            if (StringUtils.isEmpty(String.valueOf(accounts.getNature()))) {
                return error("请选择账户类型");
            } else if (!"1".equals(String.valueOf(accounts.getNature())) && !"2".equals(String.valueOf(accounts.getNature()))){
                return error("请选择正确的账户类型");
            }
            if (StringUtils.isEmpty(String.valueOf(param.get("province"))) || StringUtils.isEmpty(String.valueOf(param.get("city")))) {
                return error("服务商地区-省市不能为空");
            }  else if (Utils.IsNull(param.get("province1")) || Utils.IsNull(param.get("city1"))){
                return error("账户开户地-省市不能为空");
            }
            return toAjax(agentInfoService.saveOrUpdate(agentInfo, accounts, param));
        } catch (Exception e) {
            throw  e;
        }
    }

    /**
     * 修改服务商申请
     */
    @GetMapping("/edit")
    public String edit(ModelMap map,AgentInfo agent)
    {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agent.getAgentNum());
        map.put("agentInfo", agentInfo);
        return prefix + "/edit";
    }

    /**
     * 修改服务商申请
     */
    @GetMapping("/dw")
    public String dw(ModelMap map,@RequestParam String agentNum) {

        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoByAgentNum(agentNum);
        map.put("agentInfo", agentInfo1);
        return prefix + "/dw";
    }

    /**
     * 修改档位
     */
    @PostMapping("/editDw")
    @ResponseBody
    public AjaxResult editDw(AgentInfo agentInfo) throws Exception {

        if (agentInfo == null) {
            throw  new BusinessException("错误的请求参数");
        }

        if (StringUtils.isEmpty(agentInfo.getAgentNum())) {
            return error("请选择申请服务商");
        }
        if (StringUtils.isEmpty(agentInfo.getApplyAgentNum())) {
            return error("请选择目标服务商");
        }
        if (StringUtils.isEmpty(agentInfo.getSupportLevel())) {
            return error("请选择要提升的档位");
        }
        if (StringUtils.isEmpty(agentInfo.getSupportDate())) {
            return error("请选择扶持期限");
        }

        int i = agentInfoService.updateAgentInfo(agentInfo);
        return success("操作成功,调整" + i + "个服务商档位为" + agentInfo.getSupportLevel());
    }


    /**
     * 前往降级服务商档位页面
     */
    @GetMapping("/demotion")
    public String demotion(ModelMap map,@RequestParam String agentNum) {

        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoByAgentNum(agentNum);
        map.put("agentInfo", agentInfo1);
        return prefix + "/demotion";
    }


    /**
     * 降级指定档位
     */
    @Log(title = "降级指定档位", businessType = BusinessType.UPDATE)
    @PostMapping("/saveDemotion")
    @ResponseBody
    public AjaxResult saveDemotion(AgentInfo agentInfo) throws Exception {

        if (agentInfo == null) {
            throw  new BusinessException("错误的请求参数");
        }

        if (StringUtils.isEmpty(agentInfo.getAgentNum())) {
            return error("请选择申请服务商");
        }
        if (StringUtils.isEmpty(agentInfo.getSupportLevel())) {
            return error("请选择要提升的档位");
        }

        int i = agentInfoService.saveDemotion(agentInfo);
        return toAjax(i);
    }


    /**
     * 降下档位
     */
    @PostMapping("/rainGrade")
    @ResponseBody
    public Object rainGrade(AgentInfo agentInfo) throws Exception {

        if (agentInfo == null) {
            throw  new BusinessException("错误的请求参数");
        }
        if (StringUtils.isEmpty(agentInfo.getAgentNum())) {
            return error("请选择服务商");
        }
        int i = agentInfoService.rainGrade(agentInfo);
        return getData(i+"");
    }


    /**
     * 前往档位页面
     */
    @GetMapping("/dwOpen")
    public String dwOpen(@RequestParam Map map, ModelMap mmap)
    {
        return prefix + "/dwOpen";
    }

    /**
     * 修改保存服务商申请
     */
    @RequiresPermissions("agentInfo:agentInfo:edit")
    @Log(title = "服务商申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgentInfo agentInfo) throws ParseException {
        return toAjax(agentInfoService.updateAgent(agentInfo));
    }

    /**
     * 删除服务商申请
     */
    @RequiresPermissions("agentInfo:agentInfo:remove")
    @Log(title = "服务商申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(agentInfoService.deleteAgentInfoById(Long.parseLong(ids)));
    }


    /**
     *
     * @Description 前往选择总行页面
     * @Param [profitStat, mmap]
     * @return java.lang.String
     **/
    @GetMapping("/selectZh")
    public String selectZh(ModelMap map) {
        return prefix + "/zonghang";
    }

    /**
     *
     * @Description 前往选择经理页面
     * @Param [profitStat, mmap]
     * @return java.lang.String
     **/
    @GetMapping("/jl")
    public String jl(@RequestParam Map maps, ModelMap map) {
        map.put("obj",maps.get("obj"));
        return prefix + "/jl";
    }
    @GetMapping("/openAgentInfo")
    public String openAgentInfo() {
        return prefix + "/openAgentInfo";
    }
    /**
     *
     * @Description 前往选择经理页面
     * @Param [profitStat, mmap]
     * @return java.lang.String
     **/
    @GetMapping("/zhihang")
    public String zhihang(@RequestParam Map maps, ModelMap map) {
        map.put("head_bank",maps.get("head_bank"));
        return prefix + "/zhihang";
    }

    @RequiresPermissions("agentInfo:agentInfo:formal")
    @GetMapping("/formal")
    public String formal()
    {
        return prefix + "/formal";
    }

    /**
     * 服务商启/禁用页面跳转
     */
    @GetMapping("/changeStatus/{agentNum}")
    public String changeStatus(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoByAgentNum(agentNum);
        String agentStatus="0".equals(agentInfo.getAgentStatus())? "禁用":"正常";
        agentInfo.setAgentStatus("0".equals(agentInfo.getAgentStatus()) ? "6":"0");
        mmap.put("agentStatus", agentStatus);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/changeStatus";
    }
    /**
     * 配置推送等级
     */
    @GetMapping("/pushLevel/{agentNum}")
    public String pushLevel(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoByAgentNum(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/pushLevel";
    }

    /**
     * 保存服务商启/禁用
     */
    @RequiresPermissions("agentInfo:agentInfo:changeStatus")
    @Log(title = "服务商启/禁用", businessType = BusinessType.UPDATE)
    @PostMapping("/saveStatus")
    @ResponseBody
    public AjaxResult saveStatus(AgentInfo agentInfo)
    {
        return agentInfoService.saveStatus(agentInfo,this.getSysUser().getUserName());
    }


    /**
     *
     * @Description 前往服务商审核页面
     * @Param [profitStat, mmap]
     * @return java.lang.String
     **/
    @GetMapping("/auditView")
    public String auditView(@RequestParam Map maps, ModelMap map) {
        return prefix + "/auditView";
    }


    /**
     * 获取服务商申请列表
     */
    @RequiresPermissions("agentInfo:agentInfo:auditList")
    @PostMapping("/auditList")
    @ResponseBody
    public TableDataInfo auditList(AgentInfo agentInfo) {
        startPage();
        //获取状态为审核和打回的数据
        agentInfo.setAgentStatus("2");
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }


   /* *//**
     * 获取正式业务员
     *//*
    @RequiresPermissions("agentInfo:agentInfo:auditList")
    @PostMapping("/auditList")
    @ResponseBody
    public TableDataInfo ywy() {
        startPage();
        //查询正式业务员
        HashMap param = new HashMap();
        param.put("agent_status","1");
        List<Map> agentPageList = agentInfoService.getAgentPageList(param);
        return getDataTable(agentPageList);
    }*/



    /**
     * 前往服务商审核页面
     */
    @GetMapping("/isAudit")
    public String isAudit(ModelMap map,AgentInfo agentInfo) {
        map.put("agentNum",agentInfo.getAgentNum());
        map.put("agentStatus",agentInfo.getAgentStatus());
        return prefix + "/audit";
    }



    /**
     *
     * @Description 服务商审核
     * @Date 11:55 2020/11/23 0023
     * @Param [agent_num 服务商编号, agent_status 服务商状态, status 打回 和 通过状态 1 通过 2 打回]
     * @return com.hjm.framework.web.domain.AjaxResult
     **/
    @RequiresPermissions("agentInfo:agentInfo:audit")
    @Log(title = "服务商审核", businessType = BusinessType.DELETE)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(@RequestParam Map<String,String> param) throws Exception {
        String status = param.get("status");
        String agentNum = param.get("agentNum");
        String agentStatus = param.get("agentStatus");
        String note = param.get("note");
        try {
        if ("1".equals(status)) {
                String username = ShiroUtils.getSysUser().getObjNo();
                int result = agentInfoService.agentUpdate(agentNum, agentStatus, username, note);
                if (result > 0) {
                    if ("0".equals(agentStatus)) {
                        AgentInfo agentOne = agentInfoService.getAgentOne(agentNum);
                        HashMap mes = new HashMap();
                        mes.put("phone", agentOne.getLinkPhone());
                        mes.put("Memo", "服务商审核成功");
                        new Thread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            //TODO 审核成功后送短信通知(短信通知接口)
                                            EssentialFactors.smsCode(mes);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }
                        ).start();
                    }
                }
                return toAjax(result);
        } else if ("2".equals(status)) {
            //打回
                if (StringUtils.isEmpty(note)) {
                    return error("请描述打回原因");
                }
                if (param.get("note").length() > 100) {
                    return error("描述打回原因最多100个字符");
                }
                int result = agentInfoService.agentRepulse(agentNum, note, ShiroUtils.getSysUser().getLoginName(), agentStatus);
                return toAjax(result);
        }
        } catch (Exception e) {
            e.printStackTrace();
            return error("系统异常");
        }
        return error("请选择审核类型");
    }


    /**
     * 服务商注销页面跳转
     */
    @GetMapping("/cancelAgent/{agentNum}")
    public String cancelAgent(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        AgentInfo agentInfo= agentInfoService.selectAgentInfoByAgentNum(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/cancelAgent";
    }

    /**
     * 保存服务商注销
     */
    @PostMapping("/saveCancel")
    @RequiresPermissions("agentInfo:agentInfo:cancelAgent")
    @Log(title = "服务商注销", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult saveCancel(AgentInfo agentInfo)
    {
        return agentInfoService.saveCancel(agentInfo);
    }

    /**
     * 服务商注销重置密码
     */
    @GetMapping("/resetPwd/{agentNum}")
    public String resetPwd(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        User user = agentInfoService.getUserByObjNo(agentNum);
        mmap.put("user", user);
        return prefix + "/resetPwd";
    }

    /**
     * 修改服务商信息
     */
    @GetMapping("/updateAgentAll/{agentNum}")
    public String updateAgentAll(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/updateAgentAll";
    }

    /**
     * 代理商变更结算
     */
    @GetMapping("/onchageJs/{agentNum}")
    public String onchageJs(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/onchageJs";
    }
    /**
     * 代理商变更结算
     */
    @GetMapping("/onchageProfit/{agentNum}")
    public String onchageProfit(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/onchageProfit";
    }
    /**
     * 代理商变更是否计税
     */
    @GetMapping("/updateTaxStatus/{agentNum}")
    public String updateTaxStatus(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/updateTaxStatus";
    }

    /**
     * 代理商变更结算
     */
    @RequiresPermissions("agentInfo:agentInfo:resetPwd")
    @Log(title = "变更结算", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOnchageJs")
    @ResponseBody
    public AjaxResult updateOnchageJs(AgentInfo agentInfo) {
        if (agentInfo.getProfitStatus() == 1){
            agentInfo.setProfitStatus27(1);
        }
        int i = agentInfoService.updateAgent(agentInfo);
        return toAjax(i);
    }
    /**
     * 代理商变更结算
     */
    @Log(title = "变更结算", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOnchageProfit")
    @ResponseBody
    public AjaxResult updateOnchageProfit(AgentInfo agentInfo) {
        if (agentInfo.getDistributionStatus().equals("0") || agentInfo.getDistributionStatus().equals("1")) {
            int i = agentInfoService.updateAgent(agentInfo);
            return toAjax(i);
        } else {
            int i = agentInfoService.updateAgent1(agentInfo);
            return toAjax(i);
        }
    }
    @Log(title = "变更是否计税", businessType = BusinessType.UPDATE)
    @PostMapping("/updateTaxStatus")
    @ResponseBody
    public AjaxResult updateTaxStatus(AgentInfo agentInfo) {
        int row = agentInfoService.updateAgent(agentInfo);
        return toAjax(row);
    }



    /**
     * 代理商变更靓号
     */
    @RequiresPermissions("agentInfo:agentInfo:resetPwd")
    @Log(title = "变更靓号", businessType = BusinessType.UPDATE)
    @PostMapping("/updateBeautiful")
    @ResponseBody
    public AjaxResult updateBeautiful(AgentInfo agentInfo) {
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoByAgentNum(agentInfo.getAgentNum());
        if (agentInfo1 == null) {
            return error("错误的代理信息");
        }
        int i = agentInfoService.updateAgent(agentInfo);
        //添加记录
        beautifulRecordService.asyncInsertBeautifulRecord(agentInfo1,agentInfo);
        return toAjax(i);
    }
    @Log(title = "变更虚拟号", businessType = BusinessType.UPDATE)
    @PostMapping("/updateVm")
    @ResponseBody
    public AjaxResult updateVm(AgentInfo agentInfo) {
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoByAgentNum(agentInfo.getAgentNum());
        if (agentInfo1 == null) {
            return error("错误的代理信息");
        }
        int i = agentInfoService.updateAgent(agentInfo);
        //添加记录
        beautifulRecordService.asyncInsertBeautifulRecord(agentInfo1,agentInfo);
        return toAjax(i);
    }



    @RequiresPermissions("agentInfo:agentInfo:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(User user)
    {
        userService.checkUserAllowed(user);
        if (userService.resetUserPwd(user) > 0)
        {
            if (ShiroUtils.getUserId().equals(user.getUserId()))
            {
                setSysUser(userService.selectUserById(user.getUserId()));
            }
            return success();
        }
        return error();
    }

    /**
     * 申请盟主
     */
    @GetMapping("/setLeader/{agentNum}")
    public String setLeader(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        AgentInfo agentInfo= agentInfoService.selectAgentInfoByAgentNum(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/setLeader";
    }
    /**
     * 保存申请盟主
     */
    @RequiresPermissions("agentInfo:agentInfo:setLeader")
    @Log(title = "申请盟主", businessType = BusinessType.UPDATE)
    @PostMapping("/setLeader")
    @ResponseBody
    public AjaxResult setLeader(@RequestParam List<MultipartFile> bannerFile, String agentNum,String specialGradeId) throws Exception {
        return agentInfoService.setLeader(bannerFile,agentNum,specialGradeId,this.getSysUser().getUserName());
    }
    @GetMapping("/selectSpecialBounty")
    public String selectSpecialBounty()
    {
        return prefix + "/selectSpecialBounty";
    }

    /**
     * @Description 前往上传页面
     * @Param []
     * @return java.lang.String
     **/
    @GetMapping("/upload")
    public String upload(ModelMap map,@RequestParam String agentNum) {
        map.put("agentNum",agentNum);
        return prefix + "/upload";
    }

    @GetMapping("/findInfo")
    public String findInfo(ModelMap map,@RequestParam String agentNum) throws Exception {
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentNum);
        //查看当前代理是否是盟主,如果是盟主获取档位奖励(可能为空)
        if (agentInfo1.getAgentNature() != null && 1 == agentInfo1.getAgentNature().intValue() && agentInfo1.getSpecialGradeId() != null) {
            //获取正式盟主的引流奖励档位
            Long specialGradeId = agentInfo1.getSpecialGradeId();
            SpecialBounty specialBounty = iSpecialBountyService.selectSpecialBountyById(specialGradeId);
            map.put("specialBounty",specialBounty);
        }

        //用户信息
        // app来源资料不全 只返回基本信息
        User user = new User();
        user.setObjNo(agentInfo1.getAgentNum());
        User user1 = userService.selectUser(user);
        Accounts acc = new Accounts();
        //解密法人身份证号
        if (agentInfo1.getIdentityNum() != null) {
            agentInfo1.setIdentityNum(EncryptUtils.getDecrypt(agentInfo1.getIdentityNum()));
        }
        //手机号加*
        agentInfo1.setLinkPhone(agentInfo1.getLinkPhone().substring(0,3)+"****"+agentInfo1.getLinkPhone().substring(8,11));

        //app来源资料不全 只返回基本信息
        if ("0".equals(agentInfo1.getStatus()) && !"0".equals(agentInfo1.getAgentStatus())) {
            map.put("agentInfo",agentInfo1);
            map.put("accounts",acc);
            //用户登录账号
            //map.put("adminName",user1.getLoginName());
            return prefix + "/findInfo";
        }

        acc.setSeqId(agentInfo1.getAgentNum());
        Accounts accounts = iAccountsService.selectAccounts(acc);
        if (accounts != null) {
            if (accounts.getScreenIdnum() != null) {
                accounts.setScreenIdnum(EncryptUtils.getDecrypt(accounts.getScreenIdnum()));
            }
            //解密开户银行账户screenNum
            accounts.setScreenNum(EncryptUtils.getDecrypt(accounts.getScreenNum()));
            accounts.setResphone(EncryptUtils.getDecrypt(accounts.getResphone()));

            //账户的区域
            String bankAddress = accounts.getBankAddress();
            if (StringUtils.isNotEmpty(bankAddress)) {
                String[] split1 = accounts.getBankAddress().split(",");
                map.put("province1",split1[0]);
                map.put("city1",split1[1]);
                map.put("area1",split1.length == 3 ? split1[2] : "");
            }
            //账户信息
            map.put("accounts",accounts);
        }
        //服务商信息
        map.put("agentInfo",agentInfo1);
        //区域信息
        List<Area> pList = new ArrayList<>();
        List<Area> cList = new ArrayList<>();
        List<Area> aList = new ArrayList<>();
        List<Area> areas =redisCache.getCacheList(DictConstants.getAreaKey());
        if (areas==null){
            areas = areaService.selectAreaList(null);
            redisCache.setCacheList(DictConstants.getAreaKey(), areas);
        }
        for (Area area : areas) {
            if (area.getAreaType() == 0) {
                pList.add(area);
            } else if (area.getAreaType() == 1){
                cList.add(area);
            } else if (area.getAreaType() == 2){
                aList.add(area);
            }
        }
        map.put("pList",pList);
        map.put("cList",cList);
        map.put("aList",aList);
        //服务商的注册区域
        String areaName = agentInfo1.getAreaName();
        if (StringUtils.isNotEmpty(areaName)) {
            String[] split = areaName.split(",");
            map.put("province",split[0]);
            map.put("city",split[1]);
            map.put("area",split.length == 3 ? split[2] : "");
        }


        return prefix + "/findInfo";
    }

    /**
     * @Description 上传图片数据
     * @Param []
     * @return java.lang.String
     **/
    @PostMapping("/uploadImg")
    @ResponseBody
    public AjaxResult uploadImg(@RequestParam String agentNum,HttpServletRequest request) throws Exception {
        int result = agentInfoService.saveImg(agentNum, request, ShiroUtils.getSysUser().getObjNo());
        return toAjax(result);
    }

    /**
     * 盟主审核
     * @return
     */
    @RequiresPermissions("agentInfo:agentInfo:trialLeaderView")
    @GetMapping("/trialLeaderList")
    public String trialLeaderList()
    {
        return prefix + "/trialLeaderList";
    }

    /**
     * 正式盟主
     * @return
     */
    @RequiresPermissions("agentInfo:agentInfo:leaderView")
    @GetMapping("/leaderList")
    public String leaderList()
    {
        return prefix + "/leaderList";
    }

    /**
     * 审核盟主
     */
    @GetMapping("/trialAgent/{agentNum}")
    public String trialAgent(@PathVariable("agentNum") String agentNum, ModelMap mmap)
    {
        AgentInfo agentInfo= agentInfoService.selectAgentInfoByAgentNum(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/trialAgent";
    }


    /**
     * 审核正式服务商是否成为盟主
     * 通过 修改个人档位 和团队档位 为V9
     * radioType= 1 通过 type = 0 打回
     * @Param
     * @Return
     */
    /**
     * 保存服务商注销
     */
    @PostMapping("/trialAgent")
    @RequiresPermissions("agentInfo:agentInfo:trialAgent")
    @Log(title = "服务商注销", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult trialAgent(AgentInfo agentInfo,String radioType) {
        return agentInfoService.trialAgent(agentInfo,radioType);
    }


    /**
     * 获取服务商的图片
     */
    @PostMapping("/getImg")
    @ResponseBody
    public TableDataInfo getImg(@RequestParam String agentNum) {
        ClientImg c = new ClientImg();
        c.setObjType(Long.parseLong("1"));
        c.setClientno(agentNum);
        List<ClientImg> img = agentInfoService.getImg(c);
        return getDataTable(img);
    }

    /**
     * 查询服务商申请列表
     */
    @PostMapping("/agentList")
    @ResponseBody
    public TableDataInfo agentList(AgentInfo agentInfo)
    {
        startPage();
        agentInfo.setAgentStatus("0");
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }


    /**
     * 查询服务商申请列表(所有)
     */
    @PostMapping("/agentListAll")
    @ResponseBody
    public TableDataInfo agentListAll(AgentInfo agentInfo,@RequestParam Map map) throws Exception {
        startPage();
        Object identityNum = map.get("identityNum");
        if (identityNum != null && !identityNum.equals("")) {
            agentInfo.setIdentityNum(EncryptUtils.getEncrypt(identityNum.toString()));
        }
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoListAll(agentInfo);
        return getDataTable(agentInfos);
    }
    @PostMapping("/agentListAllNext")
    @ResponseBody
    public TableDataInfo agentListAllNext(AgentInfo agentInfo,@RequestParam Map map) throws Exception {
        startPage();
        List<AgentInfo> agentInfos = agentInfoService.selectNextAgentInfoList(agentInfo);
        return getDataTable(agentInfos);
    }
    /**
     * 查询服务商申请列表(递归)
     */
    @PostMapping("/agentListAllRecurve")
    @ResponseBody
    public TableDataInfo agentListAllRecurve(AgentInfo agentInfo,@RequestParam Map map) throws Exception {
        startPage();
        Object identityNum = map.get("identityNum");
        if (identityNum != null && !identityNum.equals("")) {
            agentInfo.setIdentityNum(EncryptUtils.getEncrypt(identityNum.toString()));
        }
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoByAgent1(agentInfo);
        agentInfo.setAgentNum(agentInfo1.getAgentNum());
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoByAgent2(agentInfo);
        return getDataTable(agentInfos);
    }

    /**
     * 新增服务商税点信息
     */
    @GetMapping("/setPoint")
    public String setPoint(@RequestParam("agentNum") String agentNum, ModelMap mmap)
    {
        TaxPoint taxPoint=new TaxPoint();
        taxPoint.setAgentNum(agentNum);
        List<TaxPoint> list=taxPointService.selectTaxPointList(taxPoint);
        if (list.size()==0){
            mmap.put("agentNum",agentNum);
            return  "agentInfo/point/add";
        }else {
            for (TaxPoint t:list) {
                taxPoint.setAgentNum(agentNum);
                if (t.getType()==0){
                    taxPoint.setTaxPoint(t.getTaxPoint());
                }else if (t.getType()==4){
                    taxPoint.setTaxPointT(t.getTaxPoint());
                }else if (t.getType()==2){
                    taxPoint.setDQtaxPoint(t.getTaxPoint());
                }else if (t.getType()==3){
                    taxPoint.setDQtaxPointT(t.getTaxPoint());
                }
            }
            mmap.put("taxPoint", taxPoint);
            return "agentInfo/point/edit";
        }
    }

    /**
     * 服务商送审
     * 送审的前提是图片资质必须上传
     * @Param [agentInfo]
     * @Return com.hjm.framework.web.page.TableDataInfo
     */
    @PostMapping("/review")
    @ResponseBody
    @Log(title = "服务商送审",businessType = BusinessType.UPDATE)
    public AjaxResult audit(AgentInfo agentInfo) {
        return toAjax(agentInfoService.audit(agentInfo));
    }


    /**
     * 代理商设置靓号
     */
    @GetMapping("/insertCode/{agentNum}")
    public String insertCode(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/insertCode";
    }
    /**
     * 代理商设置靓号
     */
    @GetMapping("/insertVm/{agentNum}")
    public String insertVm(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/insertVm";
    }

    /**
     * 设置扶持活动过期
     */
    @GetMapping("/setGradeEnd")
    public String setGradeEnd() {
        return prefix + "/setGradeEnd";
    }

    /**
     * 设置扶持活动过期
     */
    @GetMapping("/setGradeDelay/{agentNum}")
    public String setGradeDelay(@PathVariable("agentNum") String agentNum, ModelMap mmap) {
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("agentInfo", agentInfo);
        return prefix + "/setGradeDelay";
    }


    /**
     * 验证靓号
     */
    @Log(title = "验证靓号", businessType = BusinessType.UPDATE)
    @PostMapping("/valiCode")
    @ResponseBody
    public AjaxResult valiCode(AgentInfo agentInfo) throws Exception {
        if (StringUtils.isEmpty(agentInfo.getBeautiful())) {
            return AjaxResult.error("请填写9位数靓号");
        }

        //目标代理信息
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
        //注销代理商
        if (agentInfo1.getAgentStatus().equals("5")) {
            return AjaxResult.error("当前服务商已注销");
        }

        //是否是正式盟主
        if (!"0".equals(agentInfo1.getAgentStatus())) {
            return AjaxResult.error("正式服务商以上档位才可申请靓号");
        }

        //验证靓号是否使用
        AgentInfo agent = new AgentInfo();
        agent.setBeautiful(agentInfo.getBeautiful());
        AgentInfo agentInfos = agentInfoService.selectBeautiful(agent);
        if (agentInfos != null && !agentInfos.getAgentNum().equals(agentInfo1.getAgentNum())) {
            return AjaxResult.error("当前靓号已使用,请重新填写");
        }
        //防止靓号和代理编号冲突
        AgentInfo agentInfo2 = agentInfoService.selectAgentInfoByAgentNum(agentInfo.getBeautiful());
        //申请的靓号和 自己的代理商编号不相同（相同只能是自己的代理编号）
        if (agentInfo2 != null && !agentInfo2.getAgentNum().equals(agentInfo1.getAgentNum())) {
            return AjaxResult.error("不可填写服务商编号");
        }


        return AjaxResult.success();
    }


    /**
     * 验证靓号
     */
    @Log(title = "验证靓号名称", businessType = BusinessType.UPDATE)
    @PostMapping("/valiCodeName")
    @ResponseBody
    public AjaxResult valiCodeName(AgentInfo agentInfo) throws Exception {
        if (StringUtils.isEmpty(agentInfo.getBeautifulName())) {
            return AjaxResult.error("请填写靓号名称");
        }

        //目标代理信息
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
        //注销代理商
        if (agentInfo1.getAgentStatus().equals("5")) {
            return AjaxResult.error("当前服务商已注销");
        }

        //是否是正式盟主
        if (!"0".equals(agentInfo1.getAgentStatus())) {
            return AjaxResult.error("正式服务商以上档位才可申请靓号");
        }

        //验证靓号是否使用
        AgentInfo agent = new AgentInfo();
        agent.setBeautifulName(agentInfo.getBeautifulName().trim());
        List<AgentInfo> agentInfos = agentInfoService.selectAgentInfoList(agent);
        if (CollectionUtils.isNotEmpty(agentInfos) && !agentInfos.get(0).getAgentName().trim().equals(agentInfo1.getAgentName().trim())) {
            return AjaxResult.error("当前靓号名称已存在,请重新填写");
        }

        //靓号名称是否和他人服务商名称冲突
        AgentInfo agent1 = new AgentInfo();
        agent1.setAgentName(agentInfo.getBeautifulName().trim());
        List<AgentInfo> agentInfoList = agentInfoService.selectAgentInfoList(agent1);
        //申请的靓号名称不能是别人的服务商名称
        if (CollectionUtils.isNotEmpty(agentInfoList) && !agentInfoList.get(0).getAgentName().trim().equals(agentInfo1.getAgentName().trim())) {
            return AjaxResult.error("不可填写他人服务商名称,请重新填写");
        }
        return AjaxResult.success();
    }



    /**
     * 更新代理基本信息（通用所有）
     * @Param []
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @Log(title = "更新代理信息（通用所有）", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateAgent")
    @ResponseBody
    public AjaxResult updateAgent(AgentInfo agentInfo){
        return toAjax(agentInfoService.updateAgent(agentInfo));
    }


    /**
     * 更新代理基本数据
     * @Param []
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @Log(title = "更新服务商基本信息", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateAgentAll")
    @ResponseBody
    public AjaxResult updateAgentAll(AgentInfo agentInfo){
        return toAjax(agentInfoService.updateAgentAll(agentInfo));
    }


    /**
     * 服务商档位降级
     * @Param []
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @Log(title = "服务商档位降级", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateAgentRainGrade")
    @ResponseBody
    public AjaxResult updateAgentRainGrade(AgentInfo agentInfo){
        return toAjax(agentInfoService.updateAgentRainGrade(agentInfo));
    }


    /**
     * 更新扶持活动到期日
     * @Param []
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @Log(title = "更新扶持活动到期日", businessType = BusinessType.UPDATE)
    @RequestMapping("/updateAgentGrade")
    @ResponseBody
    public AjaxResult updateAgentGrade(AgentInfo agentInfo){
        return toAjax(agentInfoService.updateAgentGrade(agentInfo));
    }

    /**
     * 查找带回上送服务商
     */
    @GetMapping("/lookSendAgentInfo")
    public String lookSendAgentInfo(){
        return prefix+"/lookSendAgentInfo";
    }

    /**
     * 跳转上送服务商
     */
    @GetMapping("/sendAgentInfo")
    public String sendAgentInfo() {
        return prefix + "/sendAgentInfo";
    }


    /**
     * 跳转扣款对象
     */
    @GetMapping("/deductObject")
    public String deductObject(AgentInfo agentInfo, ModelMap mmp) {
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
        mmp.put("agentNum",agentInfo1.getAgentNum());
        mmp.put("deductObject",agentInfo1.getDeductObject());
        return prefix + "/deductObject";
    }

    /**
     * 前往设置业务类型页面
     */
    @GetMapping("/setSignAgent")
    public String setSignAgent(AgentInfo agentInfo, ModelMap mmp) {
        AgentInfo agentInfo1 = agentInfoService.selectAgentInfoById(agentInfo.getAgentNum());
        mmp.put("agentInfo",agentInfo1);
        return prefix + "/setSignAgent";
    }


    /**
     * 查找带回服务商
     * @param agentInfo
     * @return
     */
    @PostMapping("/lookSendAgentInfoList")
    @ResponseBody
    public TableDataInfo lookSendAgentInfoList(AgentInfo agentInfo){
        startPage();
        return getDataTable(agentInfoService.lookSendAgentInfoList(agentInfo));
    }

    /**
     * 调用接口上送服务商
     * @param param
     * @return
     */
    @GetMapping("/saveSendAgentInfo")
    @ResponseBody
    public AjaxResult saveSendAgentInfo(@RequestParam Map param){
        agentInfoService.sendAgentInfo(param);
        return success("正在处理中");
    }

    /**
     * 查询服务商申请列表
     */
    @PostMapping("/selectAgentListBy")
    @ResponseBody
    public TableDataInfo selectAgentListBy(AgentInfo agentInfo)
    {
        startPage();
        List<AgentInfo> agentInfos = agentInfoService.selectAgentListBy(agentInfo);
        return getDataTable(agentInfos);
    }

    /**
     * 前往代理商迁移页面
     */
    @GetMapping("/agentMigrate")
    public String agentMigrate(@RequestParam String agentNum, ModelMap mmap) throws Exception {
        //商户信息
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        String agentName = agentInfo.getAgentName();
        mmap.put("agentNum", agentNum);
        mmap.put("agentName", agentName);
        return prefix + "/agentMigrate";
    }

    /**
     * 代理商迁移
     *
     * @Param [agentNum]
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @PostMapping("/agentMigrateUpdate")
    @ResponseBody
    @Log(title = "代理商迁移", businessType = BusinessType.UPDATE)
    public AjaxResult agentMigrateUpdate(@RequestParam Map param) throws BusinessException {
        String agentNum = String.valueOf(param.get("agentNum"));
        String agentName = String.valueOf(param.get("agentName"));
        String newBelongAgent = String.valueOf(param.get("newAgentNum"));
        String newBelongAgentName = String.valueOf(param.get("newAgentName"));

        if (StringUtils.isEmpty(agentNum)) {
            return error("代理编号不能为空");
        }
        if (StringUtils.isEmpty(agentName)) {
            return error("代理编号不能为空");
        }
        if (StringUtils.isEmpty(newBelongAgent)) {
            return error("代理编号不能为空");
        }
        if (StringUtils.isEmpty(newBelongAgentName)) {
            return error("代理编号不能为空");
        }
        return agentInfoService.agentMigrateUpdate(agentNum,agentName,newBelongAgent,newBelongAgentName);
    }


    @GetMapping("/updateBankAgent/{agentNum}")
    public String updateBankAgent(@PathVariable("agentNum") String agentNum, ModelMap mmap) throws Exception {
        //商户信息
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        mmap.put("ai", agentInfo);
        //商户账户信息
        Accounts account = new Accounts();
        account.setSeqId(agentInfo.getAgentNum());
        Accounts accounts = iAccountsService.selectAccounts(account);
        //根据地区名称获取代号
        String addressName = accounts == null ? "" : accounts.getAreaName();
        String province = "";
        String city = "";
        String areaName = "";
        if (StringUtils.isNotEmpty(addressName)) {
            String[] addressArray = addressName.split(",");
            if (addressArray.length > 1) {
                if (StringUtils.isNotEmpty(addressArray[0])) {
                    province = addressArray[0];
                    Map<String, Object> params = new HashMap();
                    params.put("area_name", province);
                    params.put("area_type", "0");
                    Map<String, Object> areaCodeMap = areaService.findQBAreaCode(params);
                    province = String.valueOf(areaCodeMap.get("AREA_CODE"));
                    mmap.put("province", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
                if (StringUtils.isNotEmpty(addressArray[1])) {
                    city = addressArray[1];
                    Map<String, Object> params = new HashMap();
                    params.put("area_name", city);
                    params.put("area_type", "1");
                    Map<String, Object> areaCodeMap = areaService.findQBAreaCode(params);
                    city = String.valueOf(areaCodeMap.get("AREA_CODE"));
                    mmap.put("city", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
            }
            if (addressArray.length > 2) {
                if (StringUtils.isNotEmpty(addressArray[2])) {
                    areaName = addressArray[2];
                    Map<String, Object> params = new HashMap();
                    params.put("area_name", areaName);
                    params.put("area_type", "2");
                    Map<String, Object> areaCodeMap = areaService.findQBAreaCode(params);
                    mmap.put("area", String.valueOf(areaCodeMap.get("AREA_CODE")));
                }
            }
        }
        List<Area> pList = new ArrayList<>();
        List<Area> cList = new ArrayList<>();
        List<Area> aList = new ArrayList<>();
        List<Area> areas = areaService.selectAreaList(null);
        for (Area area : areas) {
            if (area.getAreaType() == 0) {
                pList.add(area);
            } else if (area.getAreaType() == 1 && province.equals(area.getParentId())) {
                cList.add(area);
            } else if (area.getAreaType() == 2 && city.equals(area.getParentId())) {
                aList.add(area);
            }
        }
        mmap.put("pList", pList);
        mmap.put("cList", cList);
        mmap.put("aList", aList);
        if (accounts != null && StringUtils.isNotBlank(accounts.getScreenNum())) {
            accounts.setScreenNum(EncryptUtils.getDecrypt(accounts.getScreenNum()));
        }
        if (accounts != null && StringUtils.isNotBlank(accounts.getScreenIdnum())) {
            accounts.setScreenIdnum(EncryptUtils.getDecrypt(accounts.getScreenIdnum()));
        }
        if (accounts != null && StringUtils.isNotBlank(accounts.getResphone())) {
            accounts.setResphone(EncryptUtils.getDecrypt(accounts.getResphone()));
        }
        mmap.put("accounts", accounts);
        return prefix + "/updateBankAgent";
    }

    @PostMapping("/saveUpdateBank")
    @Log(title = "商户修改结算银行", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult updateBank(@RequestParam Map<String, String> params) {
        return agentInfoService.saveUpdateBank(params);
    }


    /**
     * 前往代理商签约产品页面
     * @param agentNum
     * @param map
     * @return
     */
    @GetMapping("/setPolicy")
    public String setPolicy(@RequestParam String agentNum, ModelMap map) {
        //代理商信息
        AgentInfo agentInfo = agentInfoService.selectAgentInfoById(agentNum);
        //目标代理商产品信息
        AgentActActive aaa = new AgentActActive();
        aaa.setAgentNum(agentNum);
        List<AgentActActive> targetAgentInfo = agentActActiveService.selectagentActActiveList(aaa);
        //所有产品信息（当前登录代理）
        List<ActActive> allActList = actActiveService.selectActActiveList(null);
        for (ActActive aa : allActList) {
            for (AgentActActive a : targetAgentInfo) {
                if (a.getActId().equals(aa.getActId())) {
                    aa.setChecked(true);
                }
            }
        }
        map.put("agentInfo",agentInfo);
        map.put("pList",allActList);
        return prefix + "/setPolicy";
    }

    @PostMapping("/saveSetPolicy")
    @Log(title = "配置政策", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult saveSetPolicy(@RequestParam Map<String, String> params,String[] actId) {
        return agentInfoService.saveSetPolicy(params,actId);
    }

}
